// 参考文档地址：https://ask.dcloud.net.cn/article/992
export default {
	data() {
		return {
			currentBattery: null, //当前电量百分比
		}
	},
	methods: {
		// 获取手机电量
		getBattery() {
			return new Promise((resolve,reject)=>{
				// #ifdef APP-PLUS
				if (this.platform_mobile == 'android') {
					//注意，安卓需要配置下manifest.json文件，打开后，进入模块权限配置，在右侧的Android权限设置里勾选上android.permission.BATTERY_STATS  
					var main = plus.android.runtimeMainActivity();
					var Intent = plus.android.importClass('android.content.Intent');
					var recevier = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
						onReceive: function(context, intent) {
							var action = intent.getAction();
							if (action == Intent.ACTION_BATTERY_CHANGED) {
								var level = intent.getIntExtra("level", 0); //电量  
								var voltage = intent.getIntExtra("voltage", 0); //电池电压  
								var temperature = intent.getIntExtra("temperature", 0); //电池温度  
								//如需获取别的，在这里继续写，此代码只提供获取电量  
								//console.log(level)
								resolve(level)
							}
						}
					});
					var IntentFilter = plus.android.importClass('android.content.IntentFilter');
					var filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
					main.registerReceiver(recevier, filter);
					//
				} else if (this.platform_mobile == 'ios') {
					var UIDevice = plus.ios.import("UIDevice");  
					var dev = UIDevice.currentDevice();  
					if (!dev.isBatteryMonitoringEnabled()) {  
					    dev.setBatteryMonitoringEnabled(true);  
					}  
					var level = dev.batteryLevel();
					resolve(level)
					//
				}
				// #endif
				//#ifdef MP-WEIXIN
				wx.getBatteryInfo({
					success: function(e) {
						resolve(e.level)
					}
				})
				//#endif
			})
		}
	}
}
